package view;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import model.User;
import model.viewModels.CenterModel;
import view.viewPanels.ActGamesViewPanel;
import view.viewPanels.ArchGamesViewPanel;
import view.viewPanels.CenterViewPanel;
import view.viewPanels.DaresViewPanel;
import view.viewPanels.LoginViewPanel;

/**
 * Mainframe-Class, extends JFrame
 */
@SuppressWarnings( "serial" )
public class ChessFrame extends JFrame implements Runnable {

    /**
     * the loginViewPanel is initialized in the constructor of ChessFrame
     */
    public static LoginViewPanel loginViewPanel;
    /**
     * the basicPanel is initialized in the constructor of ChessFrame, main-panel of the frame, contains the buttons to
     * change the view and the content-panel after the login
     */
    public static BasicPanel basicPanel;
    /**
     * the centerViewPanel is initialized in the constructor of ChessFrame
     */
    public static CenterViewPanel centerViewPanel;
    /**
     * the daresViewPanel is initialized in the constructor of ChessFrame
     */
    public static DaresViewPanel daresViewPanel;
    /**
     * the actGamesViewPanel is initialized in the constructor of ChessFrame
     */
    public static ActGamesViewPanel actGamesViewPanel;
    /**
     * the archGamesViewPanel is initialized in the constructor of ChessFrame
     */
    public static ArchGamesViewPanel archGamesViewPanel;

    /**
     * the frame width after the login
     */
    public static final int FRAME_WIDTH = 1200;

    /**
     * boolean value if the update loop is running or not
     */
    public static boolean UPDATE_RUNNING;

    /**
     * shows if the frame is locked or not
     */
    public static boolean FRAME_LOCK = false;

    /**
     * a reference to the frame itself
     */
    private static ChessFrame thisFrame;

    /**
     * Constructor
     */
    public ChessFrame()
    {
	super();
	this.setTitle( "Fernschach" );
	this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	// Rahmen entfernen
	// this.setUndecorated( true );

	// initialization of the login view
	loginViewPanel = LoginViewPanel.getInstance( this );

	this.add( loginViewPanel );
	this.pack();
	// to see the title completely
	this.setSize( this.getSize().width + 5, this.getSize().height );
	this.setResizable( false );
	this.setLocationRelativeTo( null );

	this.setVisible( true );

	thisFrame = this;
	Thread thread2 = new Thread( new Runnable() {

	    @Override
	    public void run()
	    {
		boolean drawn = false;
		while ( true )
		{
		    if ( FRAME_LOCK && !drawn )
		    {
			thisFrame.repaint();
			Graphics g = thisFrame.getGraphics();
			g.setColor( new Color( 0, 0, 0, 128 ) );
			g.fillRect( 0, 0, thisFrame.getWidth(), thisFrame.getHeight() );
			drawn = true;
		    }
		    else if ( !FRAME_LOCK && drawn )
		    {
			thisFrame.repaint();
			drawn = false;
		    }
		    try
		    {
			Thread.sleep( 500 );
		    }
		    catch ( InterruptedException e )
		    {
			e.printStackTrace();
		    }

		}
	    }
	} );
	thread2.start();
    }

    /**
     * called by the loginAction of the LoginViewPanel, switches from login view to center view and invokes updates on
     * all views
     * 
     * @param loginData
     *            contains information about user name, user id and last login
     */
    public final void onLogin( String loginData )
    {
	// initialization of the views
	basicPanel = BasicPanel.getInstance( this );
	centerViewPanel = CenterViewPanel.getInstance();
	daresViewPanel = DaresViewPanel.getInstance();
	actGamesViewPanel = ActGamesViewPanel.getInstance();
	archGamesViewPanel = ArchGamesViewPanel.getInstance();

	// handle login data
	CenterModel.loggedInUser = new User( loginData.split( "#" ) );
	basicPanel.onLogin();
	updateAction();

	this.setVisible( false );
	this.remove( loginViewPanel );
	this.setSize( FRAME_WIDTH, 720 );

	this.add( basicPanel );
	this.validate();
	this.setLocationRelativeTo( null );
	this.setVisible( true );

	UPDATE_RUNNING = true;
	Thread thread = new Thread( this );
	thread.start();
    }

    /**
     * invokes updates on all views
     */
    public static final void updateAction()
    {
	// centerView
	centerViewPanel.getCenterModel().updateCenterView();

	// daresView
	daresViewPanel.getDaresModel().updateDaresData();

	// actGamesView
	actGamesViewPanel.getActGamesModel().updateActMatchData();

	// archGamesView
	archGamesViewPanel.getArchGamesModel().updateArchMatchData();
    }

    @Override
    public final void run()
    {
	try
	{
	    GregorianCalendar calendar = new GregorianCalendar();
	    // sleepTime to next full minute
	    int sleepTime = 60000 - calendar.get( Calendar.SECOND ) * 1000 - calendar.get( Calendar.MILLISECOND );
	    basicPanel.updateCurrentTime( calendar );
	    Thread.sleep( sleepTime );
	    // sleepTime 1 minute
	    sleepTime = 60000;
	    // minute counter
	    int cnt = 0;
	    // loop
	    while ( UPDATE_RUNNING )
	    {
		basicPanel.updateCurrentTime( new GregorianCalendar() );
		cnt++;
		if ( cnt == 5 )
		{
		    cnt = 0;
		    updateAction();

		}
		Thread.sleep( sleepTime );
	    }
	}
	catch ( InterruptedException e )
	{
	    e.printStackTrace();
	}
    }

}
